/*
 * Copyright (C) 2012 Roman Yakovenko <b179a9@gmail.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is furnished to do
 * so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.zi.gui.component.renderer;

import com.zi.gui.component.renderer.log.label.decorator.ErrorDecorator;
import com.zi.gui.component.renderer.log.label.decorator.NormalDecorator;
import com.zi.gui.component.renderer.log.label.decorator.SuccessDecorator;
import com.zi.gui.component.renderer.log.label.decorator.WarningDecorator;
import com.zi.gui.event.GenericEvent;

import javax.swing.*;
import java.awt.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by b17
 * 3/9/12 4:59 PM
 */

public class LogListRenderer extends JLabel implements ListCellRenderer {

    private DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

    @Override
    public Component getListCellRendererComponent(JList list, Object o, int i, boolean isSelected, boolean cellHasFocus) {
        GenericEvent event = (GenericEvent) o;
        setText(getEventText(event));

        if (isSelected) {
            setBackground(list.getSelectionBackground());
            setForeground(list.getSelectionForeground());
        } else {
            setBackground(list.getBackground());
            setForeground(list.getForeground());
        }
        setEnabled(list.isEnabled());
        setFont(list.getFont());
        setOpaque(true);

        LabelDecorator decorator = getDecorator(event.getLevel());
        decorator.decorate(this);
        return this;
    }
    
    private String getEventText(GenericEvent event) {
        Date now = new Date();
        return String.format("[%s][%s] %s", dateFormat.format(now), event.getName(), event.getMessage());
    }

    private LabelDecorator getDecorator(int errorLevel) {
        LabelDecorator decorator;
        switch (errorLevel) {
            case GenericEvent.ERROR_LEVEL:
                decorator = new ErrorDecorator();
                break;
            case GenericEvent.WARNING_LEVEL:
                decorator = new WarningDecorator();
                break;
            case GenericEvent.SUCCESS_LEVEL:
                decorator = new SuccessDecorator();
                break;
            default:
                decorator = new NormalDecorator();
        }
        return decorator;
    }
}
